package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.mapper.SensitiveMapper;
import com.heima.admin.service.SensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, AdSensitive> implements SensitiveService {


    /**
     * 分页查询敏感词数据
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getSensitive(SensitiveDto dto) {
        //判断传递过来的互数据是否为null
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //创建查询条件
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.<AdSensitive>lambdaQuery();
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        //分页
        Page<AdSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        //mybatis-plus 的分页方法
        IPage<AdSensitive> page1 = this.page(page, wrapper);

        return new PageResponseResult(dto.getPage(), dto.getSize(), page1.getTotal(),page1.getRecords());
    }


    /**
     * 添加敏感词
     * @param
     * @return
     */
    @Override
    public ResponseResult saveSensitive(AdSensitive sensitive) {
        //判断名称是否为null
        if (!StringUtils.isNotBlank(sensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"名字不能为空");
        }
        //判断敏感词是否存在
        if (count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives,sensitive.getSensitives())) > 0){
            //表示已经有这个数据了
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已存在");
        }
        //调用方法添加数据
        sensitive.setCreatedTime(new Date());
        this.save(sensitive);
        //返回成功
        return ResponseResult.okResult();
    }


    /**
     * 修改敏感词数据
     * @param sensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitive sensitive) {
        //判断数据
        if (sensitive.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"id不能为空");
        }
        //获取敏感词
        String sensitives = sensitive.getSensitives();
        if (sensitives!=null && !sensitives.equals("")){
            int count = count(new LambdaQueryWrapper<AdSensitive>().eq(AdSensitive::getSensitives, sensitive.getSensitives()));
            if (count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该敏感词已经存在");
            }
        }
        //修改数据
        updateById(sensitive);
        //返回数据
        return ResponseResult.okResult();
    }


    /**
     * 根据id删除敏感词信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        //判断参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"id不能为空");
        }
        //判断id对应的数据是否存在
        AdSensitive sensitive = getById(id);
        if (sensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"该数据不存在");
        }
        //删除数据
        this.removeById(id);
        //返回结果
        return ResponseResult.okResult();
    }


    @Autowired
    private AdSensitiveMapper adSensitiveMapper;

    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }


}
